package com.sad.jetpack.v1.thememanager.api;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;

import androidx.annotation.StyleRes;

import java.util.ArrayList;
import java.util.List;

public class TypedArrayUtils {

    public static List<Integer> getColorListFromLastStorageLocalTheme(Context context, int... attrs){
        LocalTheme localTheme = LocalTheme.getLastLocalTheme(context);
        int resId= BasicThemeDesHelper.findResIdByLocalTheme(localTheme);
        TypedArray array = context.obtainStyledAttributes(resId,attrs);
        return getColorListFromTypedArray(array,attrs);
    }

    public static List<Integer> getColorListFromCurrManifestTheme(Context context, int... attrs){
        Resources.Theme theme=context.getTheme();
        TypedArray array = theme.obtainStyledAttributes(attrs);
        return getColorListFromTypedArray(array,attrs);
    }

    public static List<Integer> getColorListFromSpecialThemeRes(Context context, @StyleRes int themeId, int... attrs){
        TypedArray array = context.obtainStyledAttributes(themeId,attrs);
        return getColorListFromTypedArray(array,attrs);
    }

    private static List<Integer> getColorListFromTypedArray(TypedArray array,int... attrs){
        List<Integer> list=new ArrayList<>();
        for (int i = 0; i < attrs.length; i++) {
            int color = array.getColor(i, 0xFFFFFF);
            list.add(color);
        }
        array.recycle();
        return list;
    }

}
